package com.sinog.front.easyexcel;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.easyexcel.listensr.ExcelIdNumJmListener;
import com.sinog.front.util.Sm4;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @desc 运维工具 - excel导入解密身份证
 * @author lzq
 * @date 2022-08-08 16:41
 */
@Slf4j
@Api(tags = "5、运维工具 - excel导入解密身份证")
@RestController
public class ExcelIdNumJmController {

    /**
     * 导入excel解密身份证
     * @param response response
     * @param columeNum 身份证所在excel的列数
     * @param file 上传文件
     */
    @ApiImplicitParams({})
    @ApiOperation("导入excel并解密身份证")
    @RequestMapping(value = "/azbj/import/idnumJm", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public void scanFilesWithNoRecursion(HttpServletResponse response,
                                           @RequestParam(value = "columeNum") int columeNum,
                                           @RequestParam(value = "file", required = false) MultipartFile file) {
        InputStream inputStream = null;
        try {
            byte[] byteArr = file.getBytes();
            String name = file.getName();
            inputStream = new ByteArrayInputStream(byteArr);
            ExcelIdNumJmListener listener = new ExcelIdNumJmListener();
            Sheet sheet = new Sheet(1,1);
            //开始读取
            new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener).read(sheet);
            List<Object> list = listener.getDatas();
            log.error(list.toString());
            //List<Object> listNew = new ArrayList<>(16);
            for(Object o : list) {
                List newList = (List)o;
                for(int j = 0; j < newList.size(); j++) {
                    String idnum = (String)newList.get(columeNum - 1);
                    if(null != idnum && Constants.Number.NUM_ZERO < idnum.length()) {
                        newList.set(columeNum - 1,Sm4.decryptEcb("",idnum));
                    }
                }
            }
            log.error(list.toString());
            try {
                ServletOutputStream out = response.getOutputStream();
                ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLSX,true);
                Sheet sheet1 = new Sheet(1,1);
                sheet1.setSheetName("sheet1");
                response.setHeader("Content-Disposition","attachment;filename=" + new String((name + "_解密" + ".xlsx").getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
                response.setContentType("application/force-download");
                writer.write(list,sheet1);
                writer.finish();
                out.flush();
            } catch(Exception e) {
                e.printStackTrace();
            }
        } catch(ExcelAnalysisException e) {
            log.error("发生异常:",e);
            Result.failure(ReturnCode.JIEXI_FAIL);
        } catch(Exception e) {
            log.error("发生异常:",e);
            Result.failure(ReturnCode.ALL_FAILURE);
        } finally {
            try {
                if(null != inputStream) {
                    inputStream.close();
                }
            } catch(IOException e) {
                log.error("发生异常:",e);
            }
        }
    }
}